using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class BlockGenerator : MonoBehaviour
{
    public List<BlockController> BlockList;
    public float TimeBetweenBlocks = 5;

    private BlockController lastBlock;
    private float countToNextBlock;


    // Use this for initialization
    void Start()
    {
      //  GenerateBlock();
    }

    // Update is called once per frame
    void Update()
    {
        if (DataManager.Instance.networkingActivated && (!DataManager.Instance.isServer || !DataManager.Instance.isRunning))
            return;
		
        if (lastBlock)
        {
            if (lastBlock.transform.position != Vector3.down)
            {
                lastBlock = null;
                countToNextBlock = TimeBetweenBlocks;
            }
        }
        else if ((countToNextBlock -= Time.deltaTime) < 0)
            GenerateBlock();
    }

    void GenerateBlock()
    {
        if (DataManager.Instance.networkingActivated)
            lastBlock = (BlockController)Network.Instantiate(BlockList[Random.Range(0, BlockList.Count)], Vector3.down, Quaternion.identity, 0);
        else
            lastBlock = (BlockController)Instantiate(BlockList[Random.Range(0, BlockList.Count)]);
    }
}
